﻿#region Description
/*  File Name:      MECameraManager.cs
 *  Author:         Adam Goodchild
 *  
 *  Description:    This is our Manager Class for our Camera's.
*/
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using MinimizeEngine.Components;
using MinimizeEngine.Entities;
#endregion

namespace MinimizeEngine.Managers
{
    public sealed class MECameraManager : GameComponent
    {
        #region Fields

        /// <summary>
        /// List of all Cameras
        /// </summary>
        private static Dictionary<string, MECameraComponent> mCameraList = new Dictionary<string, MECameraComponent>();

        /// <summary>
        /// Our Default Camera Entity
        /// </summary>
        private static MECameraComponent mDefaultCamera { get; set; }

        /// <summary>
        /// Our Active Camera Entity
        /// </summary>
        public static MECameraComponent mActiveCamera { get; private set; }

        #endregion

        #region Properties

        /// <summary>
        /// Add a new Camera to the Component Manager
        /// </summary>
        /// <param name="name">Name of the Camera</param>
        /// <param name="camera">Camera Entity</param>
        public static void AddCamera(string name, MECameraComponent camera)
        {
            if (!mCameraList.ContainsKey(name))
            {
                mCameraList.Add(name, camera);
            }
        }

        /// <summary>
        /// Set the Active Camera
        /// </summary>
        /// <param name="name"></param>
        public static void SetActiveCamera(string name)
        {
            if (mCameraList.ContainsKey(name))
            {
                mActiveCamera = mCameraList[name] as MECameraComponent;
            }
        }

        /// <summary>
        /// Set the Aspect Ratio for all Cameras. Mainly used when a Window has been Resized.
        /// </summary>
        /// <param name="aspectRatio"></param>
        public static void SetAllProjections(float aspectRatio)
        {
            foreach (MECameraComponent camera in mCameraList.Values)
            {
                camera.Parent.Projection = Matrix.CreatePerspectiveFieldOfView(camera.FOV, aspectRatio, camera.NearPlane, camera.FarPlane);
            }
        }

        #endregion

        #region Initialization
        
        /// <summary>
        /// Camera Manager Constructor
        /// </summary>
        /// <param name="gameTime"></param>
        public MECameraManager(Game game) : base(game)
        {
            MEBaseEntity cameraEntity = new MEBaseEntity("MainCamera", Vector3.Zero, Matrix.Identity);
            mDefaultCamera = new MECameraComponent(cameraEntity, (MathHelper.PiOver4), MEEngineManager.DeviceManager.GraphicsDevice.Viewport.AspectRatio, 0.5f, 100000.0f);
            mDefaultCamera.Parent.Position = new Vector3(0, 500, 300);
            mDefaultCamera.Parent.LookAt(new Vector3(0, 0, 0));

            //Add the Camera to the list of Cameras
            AddCamera("DefaultCamera", mDefaultCamera);

            //Set the Active Camera to the Default Camera
            SetActiveCamera("DefaultCamera");
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        #endregion

        #region Update

        /// <summary>
        /// Update the Active Camera
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            mActiveCamera.Update(gameTime);
        }

        #endregion
    }
}
